//
//  CollectViewController.m
//  groupProject
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CollectViewController.h"
#import "Config.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation CollectViewController
{
    UITableView *  MyTableView;
    NSMutableArray * dataArray1;  // 临时数据1
    NSMutableArray * dataArray2; // 临时数据2
    UIBarButtonItem * BackBtn;
}
@synthesize Collectsegment;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title  = @"我的收藏";
    [self addHeardBtn];
    MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 94, SCREEN_WIDTH, SCREEN_HEIGHT-49-64)];
    [self.view addSubview:MyTableView];
    MyTableView.dataSource = self;
    MyTableView.delegate = self;
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"编辑"style:UIBarButtonItemStylePlain target:self action:@selector(Edit)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    BackBtn =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(BackHome)];
    self.navigationItem.leftBarButtonItem = BackBtn;
    //临时数据
    dataArray1 = [NSMutableArray new];
    dataArray2 = [NSMutableArray new];
    for (unsigned int itemNumber = 1; itemNumber <= 12; itemNumber++)
    {
        NSString *itemName = [NSString stringWithFormat:@"一号店 %d", itemNumber];
        [dataArray1 addObject:itemName];
    }
    for (unsigned int itemNumber = 1; itemNumber <= 15; itemNumber++)
    {
        NSString *itemName = [NSString stringWithFormat:@"一号店 %d", itemNumber];
        [dataArray2 addObject:itemName];
    }
}
-(void)addHeardBtn{
    NSArray * segmentArray = [NSArray arrayWithObjects:@"店铺",@"商品", nil];
    Collectsegment = [[UISegmentedControl alloc]initWithItems:segmentArray];
    Collectsegment.frame = CGRectMake(10, 64, SCREEN_WIDTH-20, 30);
    Collectsegment.selectedSegmentIndex = _segmentIndex;
    Collectsegment.tintColor= [UIColor redColor];
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
                                             NSForegroundColorAttributeName: [UIColor whiteColor]};
    [Collectsegment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                               NSForegroundColorAttributeName: [UIColor redColor]};
    [Collectsegment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    [Collectsegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:Collectsegment];
    
}
-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger index = Seg.selectedSegmentIndex;
    NSLog(@"Index %li",(long)index);
    switch (index) {
        case 0:
            [MyTableView reloadData];
            [MyTableView setEditing:NO animated:YES];
            break;
        case 1:
            [MyTableView reloadData];
            [MyTableView setEditing:NO animated:YES];
            
            break;
        default:
            break;
    }
}
-(void)BackHome{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
-(void)Edit{
    
    [ MyTableView setEditing:!MyTableView.editing animated:YES];
}
//返回编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark -   进行删除
//进行删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * tmpArray = [[NSMutableArray alloc]init];
    if (Collectsegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Collectsegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    if (editingStyle==UITableViewCellEditingStyleDelete)
    {
        [tmpArray removeObjectAtIndex:indexPath.row];
        // 删除行
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}
#pragma mark -  进入删除模式显示删除汉子
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
#pragma mark -   显示行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (Collectsegment.selectedSegmentIndex == 0) {
        return dataArray1.count;
    }else if(Collectsegment.selectedSegmentIndex == 1)
    {
        return dataArray2.count;
    }
    return 0;
    
}
#pragma mark - 显示数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * tmpArray = [[NSMutableArray alloc]init];
    if (Collectsegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Collectsegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [tmpArray objectAtIndex:indexPath.row];
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
